package com.youthHostel.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.github.pagehelper.PageInfo;
import com.youthHostel.pojo.house;
import com.youthHostel.pojo.leasecontract;
import com.youthHostel.pojo.lessee;
import com.youthHostel.service.LeaseContractService;

@Controller
@RequestMapping("/lc")
@SessionAttributes("lcMes")
public class LeaseContractController {

	@Autowired
	private LeaseContractService leaseContractService;
	
	@RequestMapping("/lcList")
	public String huoseList(Model model,int index) {
		PageInfo<leasecontract> leaseContract = leaseContractService.findAllLc(index);
		model.addAttribute("lcMes",leaseContract);
		return "redirect:/view/leasecontractlist.jsp";
	}
	
	@RequestMapping("/add")
	public String add() {
		return "addleasecontract";
	}
	
	@RequestMapping("/addLc")
	public String addLc(leasecontract leaseContract,String bgtime, String endTime) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			leaseContract.setLc_contract_start_date(sd.parse(bgtime+" 00:00:00"));
			leaseContract.setLc_contract_end_date(sd.parse(endTime+" 23:59:59"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		leaseContract.setLc_contract_number(new Date().getTime()+"");;
		leaseContract.setLc_contract_date(new Date());
		int rs = leaseContractService.addLc(leaseContract);
		return "admin";
	}
	
	@RequestMapping("/deleteLcById")
	public String deleteLcById(int id,HttpServletRequest request) {
		int rs = leaseContractService.removeLcById(id);
//		if(rs>0) {
		return "redirect:/lc/lcList?index=1";
//		}
//		return "addHouse";
	}
	
	@RequestMapping("/findLcMesByid")
	public String findLcMesByid(int id,Model model) {
		leasecontract lessees = leaseContractService.findLcMesById(id);
		model.addAttribute("lessees",lessees);
		return "editorleasecontract";
	}
	
	@RequestMapping("/updateLcMes")
	public String updateLcMes(leasecontract leasecontracts) {
		int rs = leaseContractService.updateLc(leasecontracts);
		if(rs > 0) {
			return "admin";
		}
		return "editorLessee";
	}
	
}
